const Base = require('./base.js');
module.exports = class extends Base {
	constructor(ctx) {
		super(ctx);

		this.news = this.model("weixin/news");

	}
	async indexAction() {
		if(this.isGet) {
			return this.display();
		}
		if(this.isPost) {
			let posts = this.post();
			let where = {};

			const data = await this.news.dataTable(posts, where);
			console.log(data)
			return this.json(data);
		}

	}

	async addAction() {
		if(this.isGet) {
			return this.display();
		}
		if(this.isPost) {
			let posts = this.post();
			let user = await this.session("user");
			let insertId = await this.news.add({
				title: posts.title,
				description: posts.description,
				pic_url: posts.pic_url,
				url: posts.url,
				create_by: user.user_id,
				create_time: new Date().valueOf()

			});

			if(think.isEmpty(insertId)) {
				return this.fail("添加失败");
			}

			return this.success("添加成功");

		}

	}
	async editAction() {
		if(this.isGet) {
			let id = this.get("id");
			let news = await this.news.findOne(id);
			this.assign("news",news);
			return this.display();
		}
		if(this.isPost) {
			let posts = this.post();
			let insertId = await this.news.setOne(posts.id, {
				title: posts.title,
				description: posts.description,
				pic_url: posts.pic_url,
				url: posts.url

			});

			if(think.isEmpty(insertId)) {
				return this.fail("修改失败");
			}

			return this.success("修改成功");

		}

	}
	async imageAction(){
		let file = this.file("picture");
		
		let pics = this.uploadImage(file,"weixin");
		console.log(pics)
		return this.success(pics);
		
	}
	
	

};